function upladFile(url, form, onUploadError, onProgress, onUploadComplete) {
    var xhr = null; // XMLHttpRequest 对象
    try {
        xhr = new XMLHttpRequest();
    } catch (e) {
        xhr = ActiveXobject("Msxml12.XMLHTTP");
    }
    xhr.open("post", url, true); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
    xhr.onload = function () {
        uploadComplete(xhr.responseText, onUploadError, onUploadComplete);
    }; //请求完成
    xhr.onerror = function () {
        uploadFailed("请求错误！<br> 请检查网络或者尝试重试", onUploadError)
    }; //请求失败
    xhr.upload.onprogress = function (e) {
        progressFunction(e, onProgress);//【上传进度调用方法实现】
    };
    xhr.send(form); //开始上传，发送form数据
}

//上传进度实现方法，上传过程中会频繁调用该方法
function progressFunction(evt, onProgress) {
    var percent;
    var upladSize;
    var max, value;
    // event.total是需要传输的总字节，event.loaded是已经传输的字节。如果event.lengthComputable不为真，则event.total等于0
    if (evt.lengthComputable) {//
        max = evt.total;
        value = evt.loaded;
        upladSize = formatFileSize(value) + "&nbsp;/&nbsp;" + formatFileSize(max);
        percent = Math.round(evt.loaded / evt.total * 100);
        onProgress(max, value, percent, upladSize)
    }
}

//上传成功响应
function uploadComplete(data, onUploadError, onUploadComplete) {
    var result = eval("("+data+")");
    if (result.ret_code > 0) {
        if (onUploadComplete) {
            onUploadComplete(result);
        }
    } else {
        if (onUploadError) {
            onUploadError("哎哟，出错啦！ <br>"+result.data);
        }
    }
}
//上传失败
function uploadFailed(evt, onUploadError) {
    onUploadError(evt);
}

//将文件的单位由bytes转换为KB或MB，若第二个参数指定为true，则永远转换为KB
function formatFileSize(size, withKB) {
    if (size > 1024 * 1024 && !withKB) {
        size = (Math.round(size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
    }
    else {
        size = (Math.round(size * 100 / 1024) / 100).toString() + 'KB';
    }
    return size;
}